/*
 * font.c
 *
 *  Created on: May 19, 2010
 *      Author: abeldantas
 */

#include "font.h"
#include <io.h>
#include <stdio.h>



void initializeLetters()
{
	char* temp[] = {"space.xpm","exclam.xpm","aspas.xpm","","","","","","parleft.xpm","parright.xpm","","","virgula.xpm","","ponto.xpm","","0.xpm","1.xpm","2.xpm","3.xpm","4.xpm","5.xpm","6.xpm","7.xpm","8.xpm","9.xpm","2pontos.xpm","","","","","interr.xpm","","a.xpm","b.xpm","c.xpm","d.xpm","e.xpm","f.xpm","g.xpm","h.xpm","i.xpm","j.xpm","k.xpm","l.xpm","m.xpm","n.xpm","o.xpm","p.xpm","q.xpm","r.xpm","s.xpm","t.xpm","u.xpm","v.xpm","w.xpm","x.xpm","y.xpm","z.xpm","","","","","","","am.xpm","bm.xpm","cm.xpm","dm.xpm","em.xpm","fm.xpm","gm.xpm","hm.xpm","im.xpm","jm.xpm","km.xpm","lm.xpm","mm.xpm","nm.xpm","om.xpm","pm.xpm","qm.xpm","rm.xpm","sm.xpm","tm.xpm","um.xpm","vm.xpm","wm.xpm","xm.xpm","ym.xpm","zm.xpm"};
	
	int i=0;
	for(i=0; i!= 91; i++)
	{
		xpmLetters[i] = temp[i];
	}
}

Sprite* letterToSprite(char letter)
{
	char* filename = xpmLetters[letter - 32];
	Sprite* letterSpr = create_unbuffered_sprite_from_file(filename);
	return letterSpr;
}

void drawText_at(char* text,int x,int y)
{
	int height = 0;
	int width = 0;
	width = HRES/2-(strlen(text)*15)/2;
	int i=0;
	for(i=0; i<strlen(text); i++)
	{
	if(text[i] == '\n')
	{
		width = 0;
		height += 25;
	}else{
		Sprite* letterDraw = letterToSprite(text[i]);
		letterDraw->x = width+x;
		letterDraw->y = height+y;
		draw_sprite(letterDraw, base);
		width = width + letterDraw->width;
		//destroy_sprite(letterDraw);
		if(width>HRES)
		{
			width = 0;
			height += letterDraw->height+10;
		}
	}}
}


void destroyLetters(Sprite** letters)
{
	int i=0;
	for(i=0; i<sizeof(letters); i++)
	{
		delete_sprite(letters[i], base);
		destroy_sprite(letters[i]);
	}
}

void drawDialog(char* title,char* diag)
{

	
	int color = 0;
	int background = 90;
	int h = 100;
	int w = 350;
	if(drawFrame(title, color, background , (HRES/2)-w/2, (VRES/2)-h/2, w, h))
		drawStringAt(diag,(HRES/2)-w/2+20,(VRES/2)-h/2+30,color,background,base,table);	
		
		while(queueGet(&keys_queue)!= 28)
		{}
}

/** Draw rectangular frame with left upper corner at position x,y
* with width and height dimensions, decorated with title attributes.
* return true if sucessfull
*/
Bool drawFrame(char *title, char color, char backg, int x, int y, int width, int height)
{
	int x_fin = x + width - 1;
	int y_fin = y + height - 1;
	
	if(!printCharAt(0xC9, x, y, color, backg)) return false;			//canto superior esquerdo
	if(!printCharAt(0xBB, x_fin, y, color, backg)) return false;		//canto superior direito
	if(!printCharAt(0xC8, x, y_fin,  color, backg)) return false;		//canto inferior esquerdo
	if(!printCharAt(0xBC, x_fin, y_fin, color, backg)) return false;	//canto inferior direito
	
	int i = x + 1;
	for( ; i < x_fin; i++)
	{
		printCharAt(0xCD, i, y, color, backg);						//margem superior
		printCharAt(0xCD, i, y_fin, color, backg);					//margem inferior	
	}
	
	i = y + 1;
	for( ; i < y_fin; i++)
	{
		printCharAt(0xBA, x, i, color, backg);						//margem esquerda
		printCharAt(0xBA, x_fin, i, color, backg);					//margem direita
	}
	
	//verificar se o titulo cabe na moldura
	int title_size = 0;
	while(title[title_size] != 0) title_size++;
	if(title_size > width - 2) return false;
	
	printStringAt(title, x + 1, y + 1, color, backg);					//titulo
	return true;
}



Bool printCharAt(char ch, int x, int y, char color,char backg)
{
	
	return drawCharAt(ch,x,y, color, backg ,base,table);
}

ulong calcMemPosition(int x, int y)
{
	return VIDEO_TEXT + y * (WIDTH * 2) + x * 2;
}



Bool printStringAt(char *str, int x, int y,char color,char backg)
{
	drawStringAt(str,x,y,color,backg,base,table);
		
	return true;
}
